const  productService  = require('../../services/admin/productService');

const JWT = require('../../utils/JWT')

const productController = {
    // 添加产品逻辑
    add: async(req, res , next) => {

        const {title, description, detail } = req.body
 
        const cover = req.file ? '/productUploads/' + req.file.filename : ''
        
        await productService.add({
            updateTime: new Date(),
            title, description, detail, cover,
        }) 
        res.send({
            ActionType: "OK",
            status: 200,
            message: "添加成功",
        })
    },
    // 获取产品列表逻辑
    list: async (req, res, next) => {
        const result = await productService.list()

        res.send({
            ActionType: "OK",
            status: 200,
            data: result
        })
    },
    // 删除产品逻辑
    delete: async (req, res, next) => {
        const id = req.query.id
        await productService.delete(id)
        res.send({
            ActionType: "OK",
            status: 200,
            message: "删除成功"
        })
    },
    // 获取产品详情
    detail: async (req, res, next) => {
        const id = req.query.id
        const result =  await productService.detail(id)

        res.send({
            ActionType: "OK",
            status: 200,
            data: result
        })
    },
    // 更新产品
    update: async (req, res, next) => {
        const { title, description, detail, id } = req.body
        const cover = req.file ? '/productUploads/' + req.file.filename :  req.body.cover.split('3000')[1]
        await productService.update({
            title, description, detail, cover, _id: id ,updateTime: new Date()
        })
        res.send({
            ActionType: "OK",
            status: 200,
            message: "更新成功",
            cover
        })
    }


}

module.exports = productController;